﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using StarsAdmin.Core.App;
using StarsAdmin.Core.Consts;
using StarsAdmin.Core.DB;
using Yitter.IdGenerator;

namespace StarsAdmin.Web.Core.Extensions
{
    public static class DbContextExtension
    {
        public static void AddDbContextConfig(this IServiceCollection services)
        {
            services.AddDbContext<DBContext>(options =>
           {
               var database = AppConfigs.Sql.Database.ToUpper();
               if (database == DbTypeConst.SqlServer)
               {
                   options.UseSqlServer(AppConfigs.Sql.SqlConn);
               } else if (database == DbTypeConst.MySql)
               {
                   options.UseMySql(AppConfigs.Sql.SqlConn, new MySqlServerVersion(new Version(8, 0, 26)));
               } else if (database == DbTypeConst.Sqlite)
               {
                   options.UseSqlite(AppConfigs.Sql.SqlConn);
               }
           });
            // 初始化雪花算法
            // 保存参数（务必调用，否则参数设置不生效）：
            YitIdHelper.SetIdGenerator(AppConfigs.SnowIdOptions);
        }
    }
}